package com.fyt.algorithm.Leecode.day8_5;

public class ProductExceptSelf {
    public static int[] productExceptSelf(int[] nums){
        int length=nums.length;

        int []L=new int[length];
        int []R=new int[length];

        int []res=new int[length];
        //左前缀积   i左侧所有元素的积
        L[0]=1;
        for (int i = 1; i < length; i++) {
            L[i]=L[i-1]*nums[i-1];
        }
        //右前缀积
        R[length-1]=1;
        for (int i = length-2; i >=0 ; i--) {
            R[i]=R[i+1]*nums[i+1];
        }
        for (int i = 0; i < length; i++) {
            res[i]=L[i]*R[i];
        }
     return res;
    }

    public static void main(String[] args) {
        int[] nums={-1,1,0,-3,3};
      nums=  productExceptSelf(nums);
        for(int x:nums){
            System.out.println(x);
        }


    }
}
